package com.zbec.mdrs.service;

import com.zbec.framework.core.constant.Constants;
import com.zbec.framework.core.dao.SystemDictionaryDao;
import com.zbec.framework.core.entity.SystemDictionary;
import com.zbec.framework.core.service.SystemDictionaryTypeService;
import com.zbec.framework.security.dao.PasswordHistoryDao;
import com.zbec.framework.security.entity.PasswordHistory;
import com.zbec.mdrs.vo.ConfigPasswordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


/**
 * Created by fxl on 2015/12/3.
 */
@Component
@Transactional
public class SysPasswordService {

    @Autowired
    private SystemDictionaryDao systemDictionaryDao;

    @Autowired
    private PasswordHistoryDao passwordHistoryDao;

    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    public void configPassword(ConfigPasswordVo vo) {
        if (!StringUtils.isEmpty(vo.getDefaultPassword())) {
            SystemDictionary defaultPass = systemDictionaryDao.findOne(vo.getDefaultPasswordId());
            defaultPass.setCodeValue(vo.getDefaultPassword());
        }
        if (!StringUtils.isEmpty(vo.getExpireDays())) {
            SystemDictionary expireDays = systemDictionaryDao.findOne(vo.getExpireDaysId());
            expireDays.setCodeValue(vo.getExpireDays());
        }
        SystemDictionary loginChekc = systemDictionaryDao.findOne(vo.getLoginCheckId());
        if (!StringUtils.isEmpty(vo.getLoginCheck())) {
            loginChekc.setCodeValue("Y");
        } else {
            loginChekc.setCodeValue("N");
        }
        SystemDictionary expireCheck = systemDictionaryDao.findOne(vo.getExpireCheckId());
        if (!StringUtils.isEmpty(vo.getExpireCheck())) {
            expireCheck.setCodeValue("Y");
        } else {
            expireCheck.setCodeValue("N");
        }
        SystemDictionary samePassword = systemDictionaryDao.findOne(vo.getSamePasswordId());
        if (!StringUtils.isEmpty(vo.getSamePassword())) {
            samePassword.setCodeValue("Y");
        } else {
            samePassword.setCodeValue("N");
        }
        SystemDictionary modifyPassword = systemDictionaryDao.findOne(vo.getModifyPasswordId());
        if (!StringUtils.isEmpty(vo.getModifyPassword())) {
            modifyPassword.setCodeValue("Y");
        } else {
            modifyPassword.setCodeValue("N");
        }
    }

    public Long isPasswordWillExpire(Long userId){
        List<PasswordHistory> list= passwordHistoryDao.findUnique(userId);
        SystemDictionary defaultPassCode = this.systemDictionaryTypeService.findSystemDictionarybyTypeCode(Constants.CODE_TYPE_ACCOUNT_EXPIRE_DAYS);
        Long returnDay=0L;
        if (defaultPassCode!=null && list != null && !list.isEmpty()) {
            PasswordHistory latestPW = (PasswordHistory) list.get(0);
            Date latestDt = latestPW.getUpdatedDate();
            long daysBetween=((new Date()).getTime()-latestDt.getTime()+1000000)/(3600*24*1000);
            returnDay=Long.valueOf(defaultPassCode.getCodeValue())-daysBetween;
            if(returnDay<=5){
                return returnDay;
            }else {
                return 0L;
            }
        }
        return returnDay;
    }
}
